package com.wswy.wyjk.ui.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.support.v4.content.ContextCompat
import android.support.v7.widget.AppCompatImageView
import android.util.AttributeSet
import android.widget.TextView
import com.raft.framework.utils.UICompat
import com.wswy.wyjk.AppContext
import com.wswy.wyjk.R

class OptionImageView : AppCompatImageView {

    private companion object {

        private const val STATE_NORMAL = 0
        private const val STATE_SELECTED = 1
        private const val STATE_RIGHT = 2
        private const val STATE_WRONG = 3

    }


    constructor(context: Context) : this(context, null)

    constructor(context: Context, attribute: AttributeSet?) : this(context, attribute, 0)

    constructor(context: Context, attribute: AttributeSet?, defStyle: Int) : super(context, attribute, defStyle) {

        val typedArray = context.obtainStyledAttributes(attribute, R.styleable.OptionImageView)

        optionText = typedArray.getText(R.styleable.OptionImageView_android_text).toString()

        typedArray.recycle()
    }

    private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val rectF = RectF()

    private val sw = UICompat.dp2px(1).toFloat()

    private val strokeColor= ContextCompat.getColor(AppContext.getContext(), R.color.strokeColor)
    private val fullColor = ContextCompat.getColor(AppContext.getContext(), R.color.colorPrimary)
    private val normalTextColor = ContextCompat.getColor(AppContext.getContext(), R.color.textColorPrimary)

    private val textPaint = TextView(AppContext.getContext()).apply {

        textSize = 16f
        setTextColor(normalTextColor)

    }.paint


    private lateinit var optionText:String



    private var state = STATE_NORMAL


    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        when(state){
            STATE_NORMAL -> {
                //常规
                paint.color = strokeColor
                paint.style = Paint.Style.STROKE
                paint.strokeWidth = sw

                canvas?.drawCircle(rectF.centerX(),rectF.centerY(),rectF.width()/2f-sw*2,paint)

                textPaint.color = normalTextColor

                val x = rectF.width()/2f - textPaint.measureText(optionText)/2f
                val y = rectF.centerY() - textPaint.fontMetrics.top/2f - textPaint.fontMetrics.bottom/2f
                canvas?.drawText(optionText,x,y,textPaint)
            }

            STATE_SELECTED -> {
                //已选择
                paint.color = fullColor
                paint.style = Paint.Style.FILL

                canvas?.drawCircle(rectF.centerX(),rectF.centerY(),rectF.width()/2f,paint)

                textPaint.color = Color.WHITE

                val x = rectF.width()/2f - textPaint.measureText(optionText)/2f
                val y = rectF.centerY() - textPaint.fontMetrics.top/2f - textPaint.fontMetrics.bottom/2f
                canvas?.drawText(optionText,x,y,textPaint)
            }

            STATE_RIGHT -> {
                //正确

            }

            STATE_WRONG -> {
                //错误

            }
        }


    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)


        rectF.set(0f,0f,measuredWidth.toFloat(),measuredHeight.toFloat())

    }


    fun resetState(){
        changeState(STATE_NORMAL)
    }

    fun setNormalState(){
        changeState(STATE_NORMAL)
    }

    fun setSelectedState(){
        changeState(STATE_SELECTED)
    }

    fun setRightState(){
        changeState(STATE_RIGHT)
    }

    fun setWrongState(){
        changeState(STATE_WRONG)
    }

    fun setSelectToggle(){
        if (state == STATE_SELECTED){
            setNormalState()
        }else{
            setSelectedState()
        }
    }

    fun isOptionChecked() = state == STATE_SELECTED

    private fun changeState(state:Int){

        this.state = state

        when(state){

            STATE_RIGHT -> {
                setImageResource(R.drawable.answer_right)
            }

            STATE_WRONG -> {
                setImageResource(R.drawable.answer_error)
            }

            else -> {
                setImageResource(0)
            }
        }

        invalidate()
    }

}